package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.service.admin.AdminAdminService;
import com.cskaoyan.util.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author Bowen Deng
 * @since 2022/06/26 21:49
 */
@Service
public class AdminAdminServiceImpl implements AdminAdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    /**
     * 利用MarkAdminExample去查询Admin的list
     *
     * @param username
     * @param info
     * @return com.cskaoyan.bean.param.CommonData<com.cskaoyan.bean.MarketAdmin>
     * @author Bowen Deng
     * @since 2022/06/26 9:50 下午
     */
    @Override
    public CommonData<MarketAdmin> list(String username, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(username)) {
            username = "%" + username.trim() + "%";
            criteria.andUsernameLike(username);
        }
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        PageInfo<MarketAdmin> marketAdminPageInfo = new PageInfo<>(marketAdmins);
        CommonData data = CommonData.data(marketAdminPageInfo);
        return data;
    }

    /**
     * 添加admin，需要手动添加时间，需要查询用户名是否重复
     *
     * @param admin
     * @return java.lang.Integer
     * @author Bowen Deng
     * @since 2022/06/27 5:33 下午
     */
    @Override
    public Integer addAdmin(MarketAdmin admin) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andUsernameEqualTo(admin.getUsername());
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        if (!marketAdmins.isEmpty()) {
            return 100;
        }
        admin.setAddTime(new Date());
        admin.setDeleted(false);
        admin.setUpdateTime(new Date());
        admin.setPassword(MD5Util.MD5EncodeUtf8(admin.getPassword()));
        int affectedRows = marketAdminMapper.insert(admin);
        return affectedRows;
    }

    /**
     * 根据用户名查询指定的admin
     *
     * @param username
     * @return com.cskaoyan.bean.MarketAdmin
     * @author Bowen Deng
     * @since 2022/06/27 5:34 下午
     */

    @Override
    public MarketAdmin queryAdminByUseName(String username) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        return marketAdmins.get(0);
    }

    /**
     * 更新admin，需要手动添加修改时间，需要查询用户名是否重复
     *
     * @param admin
     * @return java.lang.Integer
     * @author Bowen Deng
     * @since 2022/06/27 5:34 下午
     */

    @Override
    public Integer updateAdmin(MarketAdmin admin) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andUsernameEqualTo(admin.getUsername());
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        if (!marketAdmins.isEmpty() && marketAdmins.get(0).getId()!=admin.getId()) {
            return 100;
        }
        admin.setUpdateTime(new Date());
        int affectedRows = marketAdminMapper.updateByPrimaryKeySelective(admin);
        return affectedRows;
    }

    /**
     * 删除admin，需要判断是否可以删除
     * 不可以删除返回100，删除成功返回1，返回0数据库异常
     *
     * @param admin
     * @return java.lang.Integer
     * @author Bowen Deng
     * @since 2022/06/27 5:34 下午
     */
    @Override
    public Integer deleteAdmin(MarketAdmin admin) {
        admin.setDeleted(true);
        Integer affectedRows = marketAdminMapper.updateByPrimaryKeySelective(admin);
        return affectedRows;
    }
}
